// 显示文章详情页模块

// 引入Article集合规则
const Article = require("../../model/article");
// 引入Comment集合规则
const Comment = require('../../model/comment.js');

module.exports = async (req, res, next) => {
    let { id } = req.query;
    try {
        // 根据id查询文章
        let result = await Article.findOne({ _id: id }).populate('writer');
        result = JSON.parse(JSON.stringify(result));
        // 根据文章的id查询当前文章的评论
        let comments = await Comment.find({ articleId: id }).populate('userId').sort('-_id');
        comments = JSON.parse(JSON.stringify(comments));
        // 渲染文章详情页面
        res.render('home/article', { article: result, comments: comments });
    } catch (error) {
        next(error);
    }
}